package com.she.iiicoursecenter.activity.tab1_infomation;

import java.util.List;

import ken.app.helper.Helper;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.TextView;

import com.she.iiicoursecenter.R;
import com.she.iiicoursecenter.activity.template.TemplateActivity;
import com.she.iiicoursecenter.event.base.Event;
import com.she.iiicoursecenter.event.connect.bean.GetContentListBean;
import com.she.iiicoursecenter.event.connect.event.GetContentListEvent;
import com.she.iiicoursecenter.event.iface.IEventCallback;
import com.she.iiicoursecenter.eventmanager.KenEventManager;
import com.she.iiicoursecenter.view.tab1.Tab1_CourseSetView;

public class Tab1_CourseSet extends TemplateActivity {
	//
	//
	private Tab1_CourseSetView		mCourseSetView;
	private ProgressDialog dialog ;
	public Tab1_CourseSet() {
		// TODO Auto-generated constructor stub
	}

	private Handler notifyHandler = new Handler(){
		@Override
		public void dispatchMessage(Message msg) {
			// TODO Auto-generated method stub
			mCourseSetView.notifyAdapter();
			dialog.cancel();
			super.dispatchMessage(msg);
		}
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		dialog = new ProgressDialog(this);
		 dialog.setMessage("讀取中...");
		 dialog.setIndeterminate(true);
		 dialog.setCancelable(true);
		 dialog.show();
		 
		// get bundle from previous...
		Bundle bundle = this.getIntent().getExtras();
		
		// test only!!!
		String name = bundle.getString("name");
		Integer itemid = bundle.getInt("itemId", -1);
//		String item3 = bundle.getString("test03");
		
		mCourseSetView = new Tab1_CourseSetView(this);
		//set title
		TextView title = (TextView) mCourseSetView.findViewById(R.id.tv_title);
		title.setText(name);
		if (name != null) {
			if (name.length()>=8) {
				title.setTextSize(18);
			}
			if (name.length()>=10) {
				title.setTextSize(18);
				title.setText(name.substring(0, 10)+"...");
			}
		}
		
		setView(mCourseSetView);
		setContentView(mCourseSetView);		
		if (getApp().getContentListBeanData().size() == 0 || (getApp().getContentListBeanData().get(0).getItemId() != itemid && itemid!=-1)) {
			GetContentListEvent getContentListEvent = new GetContentListEvent(this, itemid);
			getContentListEvent.addCallback(new IEventCallback() {
				
				public void result(Event event, Object result) {
					Tab1_CourseSet.this.getApp().setContentListBeanData((List<GetContentListBean>) result);
					notifyHandler.sendEmptyMessage(0);
				}
				
				public void progress(Event event, int progress, String tmpPath) {
				}
			});
			KenEventManager.getInstance().execute(getContentListEvent);
		}else{
			notifyHandler.sendEmptyMessage(0);
			String itemName = this.getApp().getContentListBeanData().get(0).getItemName();
			title.setText(itemName);
			if (itemName != null) {
				if (itemName.length()>=8) {
					title.setTextSize(18);
				}
				if (itemName.length()>=10) {
					title.setTextSize(18);
					title.setText(itemName.substring(0, 10)+"...");
				}
			}
		}
		
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	public void onAttachedToWindow() {
		// TODO Auto-generated method stub
		super.onAttachedToWindow();
	}

	@Override
	public void onDetachedFromWindow() {
		// TODO Auto-generated method stub
		super.onDetachedFromWindow();
	}
	
}
